/*
 * Copyright 2018-present KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nsgf.library.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.ClockInBean;
import com.nsgf.library.data.bean.VideoListBean;
import com.nsgf.library.databinding.ItemHomeVideoListBinding;
import com.nsgf.library.databinding.ItemNsgfHomeReginVideoListBinding;
import com.nsgf.library.databinding.ItemNsgfHomeVideoListBinding;
import com.nsgf.library.ui.page.video.VideoRegionInfoActivity;
import com.nsgf.library.ui.page.video.VideoRegionListActivity;

/**
 * Create by GC at 24/10/19
 */
public class ReginVideoInfoListAdapter extends SimpleDataBindingAdapter<VideoListBean.ListBean, ItemNsgfHomeReginVideoListBinding> {

    public ReginVideoInfoListAdapter(Context context) {
        super(context, R.layout.item_nsgf_home_regin_video_list, DiffUtils.getInstance().getVideoListItemCallback());

    }

    @Override
    protected void onBindItem(ItemNsgfHomeReginVideoListBinding binding, VideoListBean.ListBean item, RecyclerView.ViewHolder holder) {

        SimpleDataBindingAdapter adapter = new SimpleDataBindingAdapter<VideoListBean.ListBean, ItemHomeVideoListBinding>(mContext,
                R.layout.item_home_video_list,
                DiffUtils.getInstance().getVideoListItemCallback()) {
            @Override
            protected void onBindItem(ItemHomeVideoListBinding binding, VideoListBean.ListBean item, RecyclerView.ViewHolder holder) {
                binding.setVm(item);
                setOnItemClickListener(new OnItemClickListener<VideoListBean.ListBean>() {
                    @Override
                    public void onItemClick(int viewId, VideoListBean.ListBean item, int position) {

                        String[] points = item.getCoordinates().split(",");
                        mContext.startActivity(new Intent()

                                .putExtra("callingMode", item.getCallingMode())
                                .putExtra("deviceCode", item.getDeviceCode())
                                .putExtra("channelId",item.getChannelId())
                                .putExtra("deviceName", item.getDeviceName())
                                .putExtra("lat", points[1])
                                .putExtra("lon", points[0])
                                .setClass(mContext,
                                        VideoRegionInfoActivity.class));
                    }
                });
            }
        };
        binding.setAdapter(adapter);
        binding.recyclerViewLate.setAdapter(adapter);
        binding.setVm(item);
        adapter.notifyDataSetChanged();

        if (item.isChecked()) {
            binding.recyclerViewLate.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerViewLate.setVisibility(View.GONE);
        }
        final boolean[] rotation = {item.isChecked()};

        binding.llItemVideoTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 获取当前的旋转角度
                float currentRotation = binding.ivItemTitleUp.getRotation();

                // 计算下一个旋转角度
                float nextRotation = currentRotation + 90;

                // 创建旋转动画
                RotateAnimation rotateAnimation = new RotateAnimation(currentRotation, nextRotation,
                        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                rotateAnimation.setDuration(100); // 设置动画持续时间（例如100毫秒）
                rotateAnimation.setFillAfter(true); // 设置动画结束后保持结束状态

                // 设置动画监听器，如果需要
                rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        // 动画开始时的处理
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        // 动画结束时的处理
                        if (rotation[0]) {
                            rotation[0] = false;
                            binding.recyclerViewLate.setVisibility(View.GONE);
                        } else {
                            rotation[0] = true;
                            binding.recyclerViewLate.setVisibility(View.VISIBLE);
                        }


                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        // 动画重复播放时的处理
                    }
                });
                // 启动动画
                binding.ivItemTitleUp.startAnimation(rotateAnimation);
                // 更新当前的旋转角度
                binding.ivItemTitleUp.setRotation(nextRotation);

            }
        });

    }

}
